<?php

namespace app\model;

use app\stateData;
use think\Model;

class ClueEnterprise extends Model
{
    /**
     * 企业库
     */
    protected $createTime = 'create_date';

    public function getIds()
    {
        return $this->column ('id');
//        $list = self::field('id')->select()->toArray();
//        return array_column($list, 'id');
    }

    public function getEnterpriseTypeAttr($name)
    {
        return [
            'id' => $name ?: '',
            'text' => stateData::$_enter_prise_type[$name] ?: ''
        ];
    }

//    public function setEnterpriseTypeAttr($name)
//    {
//        return array_search($name, stateData::$_enter_prise_type, true);
//    }

    public function getFUidAttr($name): array
    {
        return [
            'id' => $name,
            'text' => (new User)->getIdNickName($name) ?: ''
        ];
    }

    public function getIsFAttr($name)
    {
        return [
            'id' => $name,
            'text' => stateData::$_is_f[$name]
        ];
    }

    public function getStatusAttr($name)
    {
        return [
            'id' => $name,
            'text' => stateData::$_enter_status[$name]
        ];
    }
//    public function getProvinceAttr(string $name): array
//    {
//        return [
//            'id'=>$name,
//            'text'=>(new SystemChina)->getCodeCityNameModel($name)?:''
//        ];
//    }
//    public function getCityAttr(string $name): array
//    {
//        return [
//            'id'=>$name,
//            'text'=>(new SystemChina)->getCodeCityNameModel($name)?:''
//        ];
//    }
    public function getAreaAttr(string $name, $data): array
    {
        //拼接省市区返回前端
        return [
            'id' => $name,
            'text' => (new SystemChina)->getAreaDataModel($data['province'], $data['city'], $name)
        ];
    }

    public function getEstablishDateAttr($value)
    {
        return $value && $value != '0000-00-00 00:00:00' ? date('Y-m-d', strtotime($value)) : '';
    }

    public function getSecureExpireDateAttr($value)
    {
        return $value && $value != '0000-00-00 00:00:00' ? date('Y-m-d', strtotime($value)) : '';
    }

    public function getAllocationDateAttr($name)
    {
        if (empty($name) || $name == '0000-00-00 00:00:00') return "";
        return $name;
    }

    public function addEnterpriseModel($data)
    {
        return self::create($data);
    }

    /**
     * 批量添加人才数据
     * @param $param
     * @return \think\Collection
     * @throws \Exception
     */
    public function addAllDataModel($param)
    {
        return $this->saveAll($param);
    }

    /**
     * 个人查询企业可见列表
     * @param $id
     * @param $type
     * @param int $page
     * @return \app\model\ClueEnterprise[]|array|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getPersonListModel($id, $type, $pageNum, int $page = 1, $ids)
    {
        return $this->where(['is_del' => 0])
            ->where(['f_uid' => $id, 'type' => $type])
            ->where('id', 'in', $ids)
            ->order(['allocation_date'=>'DESC'])
            ->page($page, $pageNum)
            ->select()->toArray();
    }
    public function getOrderDataIdModel($ids)
    {
        $result = [];
        $i=1;
        $pageNum = 50;
        while (true){
            $data = $this->where(['is_del' => 0])
                ->where('id', 'in', $ids)
                ->order(['allocation_date'=>'DESC'])
                ->page($i, $pageNum)
                ->column ('id');
            if (empty($data)){
                break;
            }
            $result = array_merge ($result,$data);
            $i++;
        }

        return $result;
    }

    public function getPersonIds($id, $type)
    {
        $list = $this->where(['is_del' => 0]);
        $list = $list->where(['f_uid' => $id, 'type' => $type])
            ->select()->toArray();
        return array_column($list, 'id');
    }

    public function getPersonCountModel($id, $type, $ids): int
    {
        return $this->where(['is_del' => 0])->where(['f_uid' => $id, 'type' => $type])->where('id', 'in', $ids)->count();
    }

    /**
     * @param $idArr
     * @param  $page
     * @param $type
     * @return \app\model\ClueEnterprise[]|array|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getManageListModel($idArr, $page, $type, $pageNum, $ids)
    {
        $idArr[] = 0;
//        return $this->whereIn('f_uid', $idArr)->where(['is_del' => 0, 'type' => $type])->where('id', 'in', $ids)->order('create_date DESC')->page($page, $pageNum)->order('create_date DESC')->select()->toArray();
        return $this->whereIn('f_uid', $idArr)
            ->where(['is_del' => 0, 'type' => $type])
            ->where('id', 'in', $ids)
            ->order(['allocation_date'=>'DESC','create_date'=>'DESC'])
//            ->page($page, $pageNum)
            ->column ('id');
    }

    public function getManageListAllModel($idArr, $type, $ids)
    {
        $idArr[] = 0;
        return $this->whereIn('f_uid', $idArr)->where(['is_del' => 0, 'type' => $type])->where('id', 'in', $ids)->order('create_date DESC')->order('create_date DESC')->column ('id');
    }

    public function getManageListIds($idArr, $type, $data = [],$param = '')
    {
        $idArr[] = 0;
        $list = $this->where ('is_del',0);
        if (!$param['org_id'] && !$param['uid']){
            $list->where('is_f',1);
        }
        if(!$data['zcqygh']){
            $list->where('is_f',2);
            $this->whereIn('f_uid', $idArr);
        }
//        $list = $list->where(['type' => $type])
//            ->select()->toArray();
//        return array_column($list, 'id');
        return $list->where(['type' => $type])->column ('id');
    }

    public function getManageCountModel($idArr, $type, $ids)
    {
        $idArr[] = 0;
        return $this->where(['is_del' => 0, 'type' => $type])->where('id', 'in', $ids)->whereIn('f_uid', $idArr)->count();
    }

    /**
     * 根据type查询回收站企业信息
     * @param $type
     * @param $page
     * @return \app\model\ClueEnterprise[]|array|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getDelEqRecycle($type, $page, $pageNum, $ids)
    {
//        return $this->where(['is_del' => 1, 'type' => $type])
//            ->where('id', 'in', $ids)
//            ->order('create_date DESC')
//            ->page($page, $pageNum)
//            ->select();
        return $this->where(['is_del' => 1, 'type' => $type])
            ->where('id', 'in', $ids)
            ->order('create_date DESC')
            ->page($page, $pageNum)
            ->column('id');
    }

    public function getDelEqRecycleAllModel($type, $ids)
    {
        return $this->where(['is_del' => 1, 'type' => $type])->where('id', 'in', $ids)->order('create_date DESC')->select()->toArray();
    }

    public function getDelEqRecycleIds($type)
    {
//        $list = $this->where(['is_del' => 1, 'type' => $type])->select()->toArray();
//        return array_column($list, 'id');
        return $this->where(['is_del' => 1, 'type' => $type])->column ('id');

    }

    public function getDelEqRecycleCount($type, $ids)
    {
        return $this->where(['is_del' => 1, 'type' => $type])->where('id', 'in', $ids)->count();
    }

    /**
     * @param $data
     * @return \app\model\ClueEnterprise
     */
    public function editEnterprise($data)
    {
        return $this->where(['id' => $data['id']])->update($data);
    }

    /**
     * @param $id
     * @return bool
     */
    public function delEnterprise($id): bool
    {
        return self::destroy($id);
    }

    /**
     * @param $id
     * @return \app\model\ClueEnterprise|array|mixed|\think\Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getIdInfo($id)
    {
        $data = $this->where(['id' => $id])->findOrEmpty();
        if (!$data->isEmpty ()){
            return $data->toArray ();
        }
        return '';
    }

    public function getEnterList($uidArr, $page, $pageNum, $ids)
    {
//        return $this->where('id', 'in', $ids)->order('create_date DESC')->select();
        return $this->where('id', 'in', $ids)->order('create_date DESC')->column('id');
    }

    public function getEnterIds($uidArr)
    {
//        $list = $this->whereIn('f_uid', $uidArr)->field('id')->select()->toArray();
//        return array_column($list, 'id');
        return $this->whereIn('f_uid', $uidArr)->field('id')->column ('id');
    }

    public function getEnterCount($ids): int
    {
        return $this->where('id', 'in', $ids)->count();
    }

    /**
     * 根据id范围查询数据
     * @param $idArr
     * @param $type
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getIdRandDataModel($idArr): array
    {
        return $this->whereIn('id', $idArr)->order('create_date DESC')->select()->toArray();
    }

    public function getPersonDataModel()
    {
        return $this->select()->toArray();
    }

    /**
     * 获取最大id
     * @return mixed
     */
    public function getMaxIdModel()
    {
        return $this->where(['is_del' => 0])->max('id');
    }

    /**
     * 查询所有分配给uid的人才信息id
     * @param $uid
     * @return array
     */
    public function getFUidDataModel($uid, $type = 1): array
    {
        return $this->where(['f_uid' => $uid, 'type' => $type])->column('id');
    }

    /**
     * 修改人才信息id集合分配人
     * @param $idArr
     * @param $fuid
     * @return \app\model\ClueEnterprise
     */
    public function editHandOverFUidModel($idArr, $fuid)
    {
        return $this->whereIn('id', $idArr)->update(['f_uid' => $fuid]);
    }

    /**
     * @param $uid
     * @param $type
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getFUidFieldDataModel($uid, $type = 1): array
    {
        return $this->where(['f_uid' => $uid, 'type' => $type])->field('id,enterprise_name as title,f_uid')->select()->toArray();
    }

    /**
     * 通过企业名称查询
     * @param $name
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getEnterpriseNameDataModel($name,$uid)
    {
        return $this->where(['enterprise_name' => $name, 'is_del' => 0])->where(['f_uid'=>$uid])->select()->toArray();
    }

    /**
     * 获取所有企业名称
     */
    public function getEnterNameDataModel($name)
    {
        return $this->whereLike('enterprise_name', '%' . $name . '%')->field('enterprise_name as title,id')->select()->toArray();
    }

    /**
     * 通过企业名称模糊搜索
     * @param $name
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getLikeNameModel($name)
    {
        return $this->whereLike ('enterprise_name','%'.$name.'%')->field('enterprise_name as title,id')->select ()->toArray ();
    }

    /**
     * 通过企业名称获取ID
     * @param $name
     * @param $uid
     * @return mixed
     */
    public function getEnterNameGetIdModel($name,$uid)
    {
        return $this->where(['enterprise_name' => $name, 'is_del' => 0])->where(['f_uid'=>$uid])->value ('id');
    }


}